﻿var PropertyPublicList = {};
$(function () {
    //初始化PropertyPublicList的属性或方法Start  
    PropertyPublicList.getEstateUrl = function () {
        var districtKeyIds = '', areaKeyIds = '', estateName = [];
        $('#hidDistrict input[type=checkbox]:checked').each(function () {
            districtKeyIds += $(this).val() + ',';
        });
        $('#hidArea input[type=checkbox]:checked').each(function () {
            areaKeyIds += $(this).val() + ',';
        });
        estateName = $('#txtEstateNames').val().split('+');
        return '/property/estates-for-region-keyids-obtain-ajax?DistrictKeyIds=' + districtKeyIds.substr(0, districtKeyIds.length - 1) + '&AreaKeyIds=' + areaKeyIds.substr(0, areaKeyIds.length - 1) + '&EstateName=' + escape(estateName[estateName.length - 1]);
    };
    //初始化PropertyPublicList的属性或方法End


    //初始化build的属性或方法Start  
    PropertyPublicList.getbuildUrl = function () {
        return '/property/building-for-estate-obatin-ajax?EstateName=' + escape($("#txtEstateNames").val()) + '&BuildName=' + $("#btnBuilding").val();
    };
    //初始化build的属性或方法End


    //楼盘自动完成及赋值隐藏域
    $("#txtEstateNames").autocomplete(PropertyPublicList.getEstateUrl, {
        width: 260,
        max: 1000,
        Default: 1,
        selectFirst: false,
        cacheLength: 0,
        multiple: true,
        multipleSeparator: '+',
        formatItem: function (row) {
            return row.EstateFullName;
        },
        formatResult: function (row) {
            return row.EstateName;

        }
    }).result(function (a, b) {
        $("#txtEstateNames").attr('isexistbuild', 'true');
        $("#txtHouseNo").removeAttr("readonly");
        $("#btnBuilding").removeAttr("readonly");
    });

    $('#txtEstateNames').bind('change', function () {
        $('#btnBuilding').val('');
        $("#hidBuildKeyId").val('');
        $('#hidBuilding ul').html('');
        $(this).attr('isexistbuild', 'true');
        if ($(this).val() == "") {
            $("#txtHouseNo").attr("readonly", "readonly").val("");
            $("#btnBuilding").attr("readonly", "readonly").val("");
        }

    });


    //栋座自动完成及赋值隐藏域
    $("#btnBuilding").autocomplete(PropertyPublicList.getbuildUrl, {
        width: 260,
        max: 1000,
        Default: 1,
        selectFirst: false,
        cacheLength: 0,
        multiple: true,
        clickMode: true,
        minChars: 0,
        formatItem: function (row) {
            return row.BuildingName;
        },
        formatResult: function (row) {

            return row.BuildingName;
        }
    }).result(function (a, b) {
        $("#hidBuildKeyId").val(b.KeyId);
    });

    $(":checkbox[name=TradeType]").unbind("click");
    $(":checkbox[name=TradeType]").bind("click", function () {
        var arr = $(":checkbox[name=TradeType]");
        $("#chbSelectAll").attr("checked", false);
        var i = 0;
        for (var j = 0; j < arr.length; j++) {
            if (arr[j].checked)
                i++;
        }
        if (i == 2) {
            $("#hidTradeType").val("");
            $("#searchForm").submit();
        }
        else {
            if ($(this)[0].checked) {
                var clickfor = $(this).attr("clickfor");
                if (clickfor == "cbkRent") {
                    $("#hidTradeType").val("2");
                }
                else if (clickfor == "cbkSale") {
                    $("#hidTradeType").val("1");
                }
            }
            else {
                if (arr[0].checked) {
                    $("#hidTradeType").val("2");
                }
                else if (arr[1].checked) {
                    $("#hidTradeType").val("1");
                }
                else {
                    $("#hidTradeType").val("");
                }
            }
            $("#searchForm").submit();
        }
    });

    $("#status").unbind("change");
    $("#status").bind("change", function () {
        $("#searchForm").submit();
    })

    //批量删除
    $("#btnBatchDelete").unbind("click");
    $("#btnBatchDelete").bind("click", function () {
        var objCheck = other.checkCount(true, "#gdList input[ad=ad]:checked", "没有选择任何数据！", "", function () {
            $.OKCancelDialogAgency('<div class="tc">您确定要删除吗？</div>', '删除提示', function () { }, function () {
                $("#btnBatchDelete").removeClass("btn-gray").addClass("btn-red");
                var tempAds = $("#gdList input[ad=ad]:checked");
                PropertyPublicList.checkAdKeyIds = "";
                for (var i = 0; i < tempAds.length; i++) {
                    if (i == 0) {
                        PropertyPublicList.checkAdKeyIds += $(tempAds[i]).attr("keyid");
                        continue;
                    }
                    PropertyPublicList.checkAdKeyIds += "," + $(tempAds[i]).attr("keyid");
                }
                var url = "/Advert/batch-remove-propertyad";
                var reqData = {
                    "adKeyIds": PropertyPublicList.checkAdKeyIds
                };
                $.AjaxAgency(url, reqData, function (data) {
                    if (data == "ok") {
                        $("#searchForm").submit();
                        $.OKDialogAgency('<div class="tc">删除成功！</div>');
                        $("#btnBatchDelete").addClass("btn-gray").removeClass("btn-red");
                    }
                    else {
                        $.OKDialogAgency('<div class="tc">' + data + '</div>');
                    }
                });
            });
        });
    });

    other.selectAll("input[name='chbSelectAll']", "#gdList .checkbox-vertical-middle", PropertyPublicList.SecondHandPropertyPublicChange);
});

//勾选checkbox删除按钮变化
PropertyPublicList.SecondHandPropertyPublicChange = function () {
    var checkedCount = 0;
    $("#gdList input[ad=ad]").each(function () {
        if ($(this).attr("checked") == "checked") {
            checkedCount = checkedCount + 1;
        }
    });
    //如果列表中都勾选了，则将全选设为勾选状态
    if ($("#gdList input[ad=ad]").length == checkedCount) {
        $("#chbSelectAll").attr("checked",true);
    }
    else {
        $("#chbSelectAll").attr("checked", false);
    }
    if (checkedCount == 1) {
        $("#btnBatchDelete").addClass("btn-red").removeClass("btn-gray");
    } else {
        $("#btnBatchDelete").removeClass("btn-red").addClass("btn-gray");
    }

    if (checkedCount > 1) {
        $("#btnBatchDelete").addClass("btn-red").removeClass("btn-gray");
    }

    if (checkedCount < 1) {
        $("#btnBatchDelete").removeClass("btn-red").addClass("btn-gray");
    }
}

function setPageStartTime() {
    window.pageStartTime = new Date();
}
function setPageEndTime() {
    $("#timeCount").html("查询耗时" + timehelper.TimeDiffirence(window.pageStartTime, new Date()).msDiff + "毫秒");
}

var loadscript = {};

loadscript.beginSearch = function () {
    //此方法在分页控件中
    if ("undefined" != typeof ajaxpageronbegin) {
        ajaxpageronbegin();
    }
    setPageStartTime();
};
loadscript.pageOnSuccessFun = function () {
    setPageEndTime();
};